﻿using System.Collections.Generic;
using CiStatus.CiStatusAddIn.Logic.Exceptions;
using CiStatus.CiStatusAddIn.Logic.Parameters;
using CiStatus.CiStatusAddIn.Logic.PluginInterfaces;

namespace TeamCity
{
    public class TeamCityPollerFactory : ICiPollerFactory
    {
      public ICiPoller CreatePoller(IDictionary<string, string> settings)
        {
            var ret = CreateEmptyParameters();
            ret["Server"].SetValue(GetValue(settings, "Server"));
            ret["Project"].SetValue(GetValue(settings, "Project"));
            ret["SubProject"].SetValue(GetValue(settings, "SubProject"));
            ret["Link"].SetValue(GetValue(settings, "Link"));
            ret["User"].SetValue(GetValue(settings, "User"));
            ret["Password"].SetValue(GetValue(settings, "Password"));
            return new TeamCityPoller(ret);
        }

        private string GetValue(IDictionary<string, string> settings, string setting)
        {
            string server;
            settings.TryGetValue(setting, out server);

            if (!string.IsNullOrEmpty(server))
            {
                return settings[setting];
            }
            throw new CreatePollerException(string.Format("Error in setting key: {0}", setting));
        }

        public string Name
        {
            get { return "TeamCity"; }
        }

        public IDictionary<string, ICiPollerParameter> EmptyParameters
        {
            get { return CreateEmptyParameters(); }
        }

        public IDictionary<string, ICiPollerParameter> CreateEmptyParameters()
    	{
			var ret = new Dictionary<string, ICiPollerParameter>();
            ret["Server"] = new SimpleKeyValueStringParameter(string.Empty);
            ret["Project"] = new SimpleKeyValueStringParameter(string.Empty);
            ret["SubProject"] = new SimpleKeyValueStringParameter(string.Empty);
            ret["Link"] = new SimpleKeyValueStringParameter(string.Empty);
            ret["User"] = new SimpleKeyValueStringParameter(string.Empty);
            ret["Password"] = new SimpleKeyValueStringParameter(string.Empty);
			return ret;
    	}
    }
}
